#include "app_bluetooth.h"
#include "log/log.h"
#include <stdio.h>


static int wait_ack(Device *device){
    usleep(1000*50);
    char buffer[4];
    read(device->fd,buffer,4);
    if(memcmp(buffer,"OK\r\n",4)==0)
    {
        return 0;
    }
    else
    {
        log_error("Error: %s",buffer);
        return -1;
    }
}
int app_bluetooth_status(Device *device){
    write(device->fd,"AT",2);

    return wait_ack(device);
}
int app_bluetooth_rename(Device *device,char *name)
{
    write(device->fd,"AT+NAME",7);
    write(device->fd,name,strlen(name));
    write(device->fd,"\r\n",2);
    return wait_ack(device);
}
int app_bluetooth_setBraudRate(Device *device,BTbraud br){
    write(device->fd,"AT+BAUD",7);
    write(device->fd,br,1);
    write(device->fd,"\r\n",2);
    return wait_ack(device);
}
int app_bluetooth_reset(Device *device){
    write(device->fd,"AT+RESET",8);
    write(device->fd,"\r\n",2);
    return wait_ack(device);
}
int app_bluetooth_setNetID(Device *device,char *netID){
    write(device->fd,"AT+NETID",8);
    write(device->fd,netID,strlen(netID));
    write(device->fd,"\r\n",2);
    return wait_ack(device);
}
int app_bluetooth_setMaddr(Device *device,char *maddr){
    write(device->fd,"AT+MADDR",8);
    write(device->fd,maddr,strlen(maddr));
    write(device->fd,"\r\n",2);
    return wait_ack(device);
}

//处理读取的蓝牙数据的函数
int app_bluetooth_postRead(Device *device,char *data,int len);
//处理将要写入的蓝牙数据的函数
int app_bluetooth_preWrite(Device *device,char *data,int len);